//安装依赖包
//npm install @alicloud/facebody20191230
// 最低SDK版本要求：facebody20191230的SDK版本需大于等于4.0.7
// 可以在此仓库地址中引用最新版本SDK：https://npmjs.com/package/@alicloud/facebody20191230
const accessKeyId = 'LTAI5tB9qrhKx9TZh6V9yx8i';
const accessKeySecret = '4bXvx0hqFmtYnTsQuR4GjeRI4m1UKJ';

const FacebodyClient = require('@alicloud/facebody20191230');
const OpenapiClient = require('@alicloud/openapi-client');
const TeaUtil = require('@alicloud/tea-util');
const logger = require('./Logger').getLogger('ossAi');
const fs = require('fs');
const {getAllConfig} = require('./DataManager');

let config = new OpenapiClient.Config({
    // 创建AccessKey ID和AccessKey Secret，请参考https://help.aliyun.com/document_detail/175144.html。
    // 如果您用的是RAM用户AccessKey，还需要为RAM用户授予权限AliyunVIAPIFullAccess，请参考https://help.aliyun.com/document_detail/145025.html。
    // 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行示例前必须先配置环境变量。 
    accessKeyId,
    accessKeySecret
});
// 访问的域名
config.endpoint = `facebody.cn-shanghai.aliyuncs.com`;
const client = new FacebodyClient.default(config);

const request = async function (imgPath) {
    try {
        let generateHumanAnimeStyleAdvanceRequest = new FacebodyClient.GenerateHumanAnimeStyleAdvanceRequest({
            algoType: getAllConfig().algoType, //artstyle
        });
        // 场景一，使用本地文件
        const fileStream = fs.createReadStream(imgPath);
        generateHumanAnimeStyleAdvanceRequest.imageURLObject = fileStream;

        // 场景二，使用任意可访问的url
        // const url = new URL('http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/GenerateHumanAnimeStyle/GenerateHumanAnimeStyle8.png');
        // const httpClient = (url.protocol == "https:") ? https : http;
        // generateHumanAnimeStyleAdvanceRequest.imageURLObject = await getResponse(httpClient, url);
        let runtime = new TeaUtil.RuntimeOptions({});
        return await client.generateHumanAnimeStyleAdvance(generateHumanAnimeStyleAdvanceRequest, runtime)
    } catch (error) {
        console.log(error, 'error');
        logger.error(error);
        return error
    }
    // console.log(res.body.data.imageURL);
}
// request()
module.exports = {
    ossAiRequest: request
}